package com.lindroy.androidplayer.viewmodel

import androidx.lifecycle.MutableLiveData
import com.lindroy.androidplayer.base.viewmodel.BaseViewModel
import com.lindroy.androidplayer.extensions.judgeLogin
import com.lindroy.androidplayer.extensions.no
import com.lindroy.androidplayer.extensions.request
import com.lindroy.androidplayer.extensions.yes
import com.lindroy.androidplayer.logic.model.CollectArticleResp
import com.lindroy.androidplayer.logic.network2.apiService
import com.lindroy.androidplayer.logic.network2.statecallback.CollectState
import com.lindroy.androidplayer.logic.network2.statecallback.ListDataState

/**
 * @author Lin
 * @date 2021/4/16
 * @function 收藏相关的ViewModel
 */
class CollectViewModel : BaseViewModel() {
    /**
     * 页码，从0开始
     * 页码放在ViewModel中保存
     */
    private var pageNo = 0

    val collectState: MutableLiveData<CollectState> = MutableLiveData()

    /**
     * 收藏文章列表
     */
    val collectedArticlesState: MutableLiveData<ListDataState<CollectArticleResp>> =
        MutableLiveData()

    /**
     * 收藏
     */
    fun collectArticle(id: Int) {
        request({ apiService.collect(id) }, {
            collectState.value = CollectState(success = true, collect = true, id = id,originId = id)
        }, {
            collectState.value = CollectState(success = false, collect = false, id = id,originId = id)
        }, isShowDialog = true, loadingMessage = "正在收藏...")
    }

    /**
     * 取消收藏-我的收藏页面
     * 必须传入originId，首页文章才能同步删除
     */
    fun unCollectArticle(id: Int, originId: Int) {
        request({ apiService.unCollect(id, originId) }, {
            collectState.value =
                CollectState(success = true, collect = false, id = id, originId = originId)
        }, {
            collectState.value =
                CollectState(success = false, collect = true, id = id, originId = originId)
        }, isShowDialog = true, loadingMessage = "正在取消收藏...")
    }

    /**
     * 取消收藏-文章列表
     */
    fun unCollectListArticle(id: Int) {
        request({ apiService.unCollectListArticle(id) }, {
            collectState.value =
                CollectState(success = true, collect = false, id = id, originId = id)
        }, {
            collectState.value =
                CollectState(success = false, collect = true, id = id, originId = id)
        }, isShowDialog = true, loadingMessage = "正在取消收藏...")
    }

    /**
     * 判断收藏还是取消收藏
     */
    fun toggleCollectListArticle(collect:Boolean,id: Int){
        judgeLogin {
            collect.yes {
                unCollectListArticle(id)
            }.no {
                collectArticle(id)
            }
        }
    }

    /*fun collectUrl(name:String,link:String){
        request({ apiService.collectUrl(name, link)},{
            collectState.value =
                CollectState(success = true, collect = false, id = id, originId = id)
        })
    }*/

    /**
     * 获取收藏的文章列表
     */
    fun getCollectedArticles(isRefresh: Boolean) {
        isRefresh.yes { pageNo = 0 }
        request({ apiService.getCollectedArticles(pageNo) }, {
            collectedArticlesState.value = ListDataState(
                success = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.datas
            )
            pageNo++
        }, {
            collectedArticlesState.value = ListDataState(
                success = false,
                isRefresh = isRefresh,
                errMessage = it.errorMsg,
                listData = arrayListOf()
            )
        })
    }
}